package service;

import dao.DvdDao;
import dao.DvdDaoImpl;
import pojo.DVD;

import java.time.LocalDate;
import java.util.Scanner;

public class DvdService {
    DvdDao dao = new DvdDaoImpl();
    //将dvd的业务逻辑放于此处

    //添加的业务逻辑
    public void saveDvd(Scanner scanner){
        System.out.println("请输入新增DVD名字");
        String name = scanner.next();
        System.out.println("请输入DVD租金");
        double rent = scanner.nextDouble();
        DVD dvd = new DVD(name);
        dvd.setState(1);
        dvd.setRent(rent);
        boolean flag = dao.insertDVD(dvd);
        if (flag) {
            System.out.println("新增成功");
        } else {
            System.out.println("货架已满，新增失败!!!!!");
        }
    }

    //查询展示的业务逻辑
    public void showDvd(Scanner scanner){
        System.out.println("---------> 查看DVD信息");
        System.out.println("序号\t 状态 \t 名称\t借出次数\t借出日期");
        DVD[] dvds = dao.queryAll();
        int count = 1;
        for (int j = 0; j < dvds.length; j++) {
            System.out.println(j + 1 + "\t " +
                    (dvds[j].getState() == 0 ? "已借出" : "未借出") + "\t" +
                    dvds[j].getName() + "\t " +
                    dvds[j].getCount() + "\t  " +
                    (dvds[j].getDate() == null ? " " : dvds[j].getDate()));
            count++;
        }
    }

    //删除的业务逻辑
    public void deleteDvd(Scanner scanner){
        System.out.println("请输入要删除的DVD名字");
        String name = scanner.next();
        boolean flag = dao.deleteDVD(name);
        if (flag) {
            System.out.println("删除成功!!!!");
        } else {
            System.out.println("没有找到对应dvd信息，删除失败!!!!!!");
        }
    }

    //借出的业务逻辑
    public void lendDvd(Scanner scanner){
        System.out.println("请输入要借阅的DVD名字");
        String name = scanner.next();
        DVD dvd1 = dao.queryByName(name);
        if (dvd1 == null) {
            System.out.println("DVD不存在");
        } else {
            if (dvd1.getState() == 0) {
                System.out.println("DVD已借出");
            } else {
                boolean flag = dao.lendDVD(new DVD(name));
                if (flag) {
                    System.out.println("借阅成功");
                } else {
                    System.out.println("借阅失败");
                }
            }
        }
    }


    //归还的业务逻辑
    public void revestDvd(Scanner scanner){
        System.out.println("请输入要归还的DVD名字");
        String name = scanner.next();
        DVD dvd1 = dao.queryByName(name);
        if (dvd1 == null){
            System.out.println("没有此DVD");
        }else {
            if (dvd1.getState() == 0){
                int day = LocalDate.now().getDayOfMonth() - dvd1.getDate().getDayOfMonth();
                System.out.println("需要支付租金：" + ((day + 1) * dvd1.getRent()));
                dao.revert(new DVD(name));
            }else {
                System.out.println("当前DVD未被借出");
            }
        }
    }

}
